import { TileWMS } from 'ol/source';
import TileLayer from "ol/layer/Tile";
import { fromArrayBuffer } from "geotiff";


/**
 *  geotiff和wms管理类  （部分功能待开发，具体根据后续需求）
 */
export default class tifHanler {
  private wmsSource?: TileWMS;



  /**
   * 可以使用
   * 加载wms geotiff
   * @param url wms服务地址
   * @param params 
   */
  public initWms(url: string, params: { [key: string]: any }): TileLayer {
    this.wmsSource = new TileWMS({
      url,
      params,
      serverType: "geoserver"
    })
    const tileLayer = new TileLayer({
      source: this.wmsSource
    })

    return tileLayer
  }



  /**
   * 测试中未使用
   * 用于获取 TIF 数据  
   * @param url tif远程链接
   * @returns 返回二进制缓冲区
   */
  public async fetchTifData(url: string) {
    const response = await fetch("/data/tif/home/EC_tiff-4326/mifeng/240811182844641118_ECMWF_D1D_FOR_GLB_U_20240811120000_24_`100`_`92500`_192-168-20-104.tif")
    const arrayBuffer = await response.arrayBuffer()
    const tiff = await fromArrayBuffer(arrayBuffer)
    const image = await tiff.getImage();
    // const width = image.getWidth();
    // const height = image.getHeight();
    const tileWidth = image.getTileWidth();
    const tileHeight = image.getTileHeight();
    const samplesPerPixel = image.getSamplesPerPixel();

    // when we are actually dealing with geo-data the following methods return
    // meaningful results:
    const origin = image.getOrigin();
    const resolution = image.getResolution();
    const bbox = image.getBoundingBox();

    const data = await image.readRasters();
    const { width, height } = data;

    const [red, green, blue] = await image.readRasters();
  }
}
